package ms.unicorn.erml.edit.policies;

import java.util.Collection;
import java.util.List;

import ms.unicorn.diagram.edit.policies.UnicornBaseItemSemanticEditPolicy;
import ms.unicorn.modellanguage.HintedSpecializationType;
import ms.unicorn.modellanguage.LanguageElement;
import ms.unicorn.modellanguage.LanguageEntity;

import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;

public class EntityCompartmentItemSemanticEditPolicy extends
		UnicornBaseItemSemanticEditPolicy {

	private LanguageEntity langEntity;
	
	public EntityCompartmentItemSemanticEditPolicy(LanguageEntity langEntity) {
		super(langEntity.getElementType());
		this.langEntity = langEntity;
	}
	
	@Override
	protected Command getCreateCommand(CreateElementRequest req) {
		IElementType type = req.getElementType();
		if (type instanceof HintedSpecializationType) {
			HintedSpecializationType hintType = (HintedSpecializationType) type;
			LanguageElement e = hintType.getElement();
			if (e instanceof LanguageEntity && langEntity.acceptChild(e)) {
				ICommand creationCommand =  ((LanguageEntity)e).getEditHelperAdvice().getCreationCommand(hintType.getElement(), req);
				if (creationCommand != null)
					return getGEFWrapper(creationCommand);
			}
		}
		return super.getCreateCommand(req);
	}
}
